// ==========================================================================
// gin-sns自动生成控制器相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:53 +0800 CST
// path: app/controller/module/category_api.go
// author：jon
// ==========================================================================
package module

import (
	"gin-sns/app/controller/api/cache"
	"gin-sns/app/controller/api/render"
	"gin-sns/app/ginframe/response"
	"gin-sns/app/model"
	categoryModel "gin-sns/app/model/module/category"
	"gin-sns/app/service/module"

	"github.com/gin-gonic/gin"
	//"gin-sns/app/ginframe/utils/gconv"
)

var CategoryApi = newCategoryApi()

func newCategoryApi() *categoryApi {
	return &categoryApi{}
}

type categoryApi struct {
}

//列表分页数据
func (c *categoryApi) ListAjax(ctx *gin.Context) {
	req := new(categoryModel.SelectPageReq)
	//获取参数
	if err := ctx.ShouldBind(req); err != nil {
		response.ErrorResp(ctx).SetMsg(err.Error()).Log("category管理", req).WriteJsonExit()
		return
	}
	rows := make([]categoryModel.Entity, 0)
	result, page, err := module.CategoryService.SelectListByPage(req)

	if err == nil && len(result) > 0 {
		rows = result
	}

	response.BuildTable(ctx, page.Total, rows).WriteJsonExit()
}

//新增页面保存
func (c *categoryApi) AddSave(ctx *gin.Context) {
	req := new(categoryModel.AddReq)
	//获取参数
	if err := ctx.ShouldBind(req); err != nil {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Add).SetMsg(err.Error()).Log("分类新增数据", req).WriteJsonExit()
		return
	}

	id, err := module.CategoryService.AddSave(req, ctx)

	if err != nil || id <= 0 {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Add).Log("分类新增数据", req).WriteJsonExit()
		return
	}
	response.SucessResp(ctx).SetData(id).Log("分类新增数据", req).WriteJsonExit()
}

//修改页面保存
func (c *categoryApi) EditSave(ctx *gin.Context) {
	req := new(categoryModel.EditReq)
	//获取参数
	if err := ctx.ShouldBind(req); err != nil {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).Log("分类修改数据", req).WriteJsonExit()
		return
	}

	rs, err := module.CategoryService.EditSave(req, ctx)

	if err != nil || rs <= 0 {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Edit).Log("分类修改数据", req).WriteJsonExit()
		return
	}
	response.SucessResp(ctx).SetBtype(model.Buniss_Edit).Log("分类修改数据", req).WriteJsonExit()
}

//删除数据
func (c *categoryApi) Remove(ctx *gin.Context) {
	req := new(model.RemoveReq)
	//获取参数
	if err := ctx.ShouldBind(req); err != nil {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Del).SetMsg(err.Error()).Log("分类删除数据", req).WriteJsonExit()
		return
	}

	rs := module.CategoryService.DeleteRecordByIds(req.Ids)

	if rs > 0 {
		response.SucessResp(ctx).SetBtype(model.Buniss_Del).Log("分类删除数据", req).WriteJsonExit()
	} else {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Del).Log("分类删除数据", req).WriteJsonExit()
	}
}

//GET /{id}
func (c *categoryApi) GetBy(ctx *gin.Context) {
	categoryId := ctx.GetInt64("id")
	category := cache.CategoryCache.Get(categoryId)

	if category == nil {
		response.ErrorResp(ctx).SetMsg("分类不存在").WriteJsonExit()
		return
	}
	response.SucessResp(ctx).SetData(render.BuildCategory(category)).WriteJsonExit()
	return
}

//GET /article
func (c *categoryApi) GetArticle(ctx *gin.Context) {
	//page := ctx.GetInt("page")

	list, _ := module.CategoryService.GetNewestCategories(model.CategoryTypeArticle, 200, 0)

	response.SucessResp(ctx).SetData(render.BuildCategories(list)).WriteJsonExit()
	return
}

//GET /feed
func (c *categoryApi) GetFeed(ctx *gin.Context) {
	//page := ctx.GetInt("page")

	list, _ := module.CategoryService.GetNewestCategories(model.CategoryTypeFeed, 200, 0)

	response.SucessResp(ctx).SetData(render.BuildCategories(list)).WriteJsonExit()
	return
}
